﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BranchOfSansar.Sansar.Register;
using System.ServiceModel;
using DevExpress.XtraEditors;
namespace BranchOfSansar.Register.Main
{
    public partial class frmCustomer : Form
    {
        private Customer cust=new Customer();
        public frmCustomer()
        {
            InitializeComponent();
        }

        private void frmCustomer12_Load(object sender, EventArgs e)
        {
            loadEdit();
        }

        private void panelControl1_Paint(object sender, PaintEventArgs e)
        {

        }
        private void loadEdit()
        {
            cmbStatus.SelectedIndex = 0;
            cmbType.SelectedIndex = 0;
            if (this.Tag.ToString().Equals("new"))
            {
                if (this.Tag == "new")
                {
                    cmbStatus.Visible = false;
                }
                cust = new Customer();
            }
            else
            {
                using (RegisterClient obj = new RegisterClient(Globals.Token))
                {
                    vw_Customers vw_cust= obj.GetCustomer(Convert.ToInt32(this.Tag));
                    cust.CustNo = vw_cust.CustNo;
                    txtAddition.Text = vw_cust.ExtraInfo;

                    txtApartment.Text = vw_cust.ApartCode;
                    loadApartment();
                    txtToot.Text = vw_cust.DoorNo;
                    txtCode.Text = vw_cust.CustCode;
                    txtMail.Text = vw_cust.Email;
                    txtName.Text = vw_cust.CustName;
                    txtPhone.Text = vw_cust.Phones;
                    txtReg.Text = vw_cust.CustRegister;
                    cmbType.SelectedIndex = vw_cust.CustType;
                    cmbStatus.SelectedIndex = vw_cust.Status;
                }
            }
        }

        private void saveApartment()
        {
            if (!Globals.DoValidation(panelControl2))
            {
                return;
            }
            cust.ExtraInfo = txtAddition.Text;
            cust.ApartNo = Convert.ToInt32(txtApartment.Tag);
            cust.DoorNo = txtToot.Text;
            cust.CustCode = txtCode.Text;
            cust.Email = txtMail.Text;
            cust.CustName = txtName.Text;
            cust.Phones = txtPhone.Text;
            cust.CustRegister = txtReg.Text;
            if (this.Tag == "new")
            {
                cust.CustType = 0;
            }
            else
            {
                cust.CustType = cmbType.SelectedIndex;
            }
            cust.Status = cmbStatus.SelectedIndex;

            using (RegisterClient obj = new RegisterClient(Globals.Token))
            {
                Result r;
                if (this.Tag == "new")
                {
                    r = obj.AddCustomer(cust);
                }
                else
                {
                    r = obj.EditCustomer(cust);
                }
                if (r.Code != 0)
                {
                    Globals.Err(r.Message);
                    return;
                }
                cust.CustNo = long.Parse(r.RetData.ToString());
                //Globals.Info(r.Message);
                if (this.Tag == "new")
                {
                    if (Globals.Question("Амжилттай бүртгэлээ, шинэ харилцагчид гэрээ үүсгэх үү?") == System.Windows.Forms.DialogResult.Yes)
                    {
                        frmContract frm = new frmContract();
                        frm.Tag = "new";
                        frm.txtCustReg.Text = cust.CustRegister;
                        frm.txtCustReg.Tag = cust.CustNo;
                        frm.txtCustName.Text = cust.CustName;
                        frm.txtApartment.Text = txtApartment.Text;
                        frm.txtToot.Text = cust.DoorNo;
                        frm.txtPhone.Text = cust.Phones;
                        frm.txtMail.Text = cust.Email;
                        frm.loadApartment();
                        frm.ShowDialog(this);
                    }
                }
                DialogResult = System.Windows.Forms.DialogResult.OK;

            }

        }

        private void cmdSave_Click(object sender, EventArgs e)
        {
            saveApartment();
            //cmdSave.Enabled = false;
        }

        private void cmdExit_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            frmApartmentList frm = new frmApartmentList();
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.Yes)
            {
                txtApartment.Text = frm.SelectedItem.ApartCode.ToString();
                txtApartment.Tag = frm.SelectedItem.Id;
                txtApartName.Text = frm.SelectedItem.Description;
            }
        }

        private void txtApartment_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void txtApartment_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                loadApartment();
            }
        }
        private void loadApartment()
        {
            using (RegisterClient obj = new RegisterClient(Globals.Token))
            {
                Apartment ap = obj.GetApartmentByCode(txtApartment.Text);
                if (ap == null)
                {
                    Globals.Info(txtApartment.Text + " кодтой байр байхгүй байна!");
                    txtApartName.Text = "";
                    txtApartment.Tag = "";
                }
                else
                {
                    txtApartName.Text = ap.Description;
                    txtApartment.Tag = ap.Id.ToString();
                }
            }
        }
    }
}
